package androidgames.networkgames.screens.components;

import java.util.ArrayList;

import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Graphics.PixmapFormat;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.Sound;
import com.badlogic.androidgames.framework.impl.AndroidPixmap;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import androidgames.configs.UIConfig;
import androidgames.networkgames.screens.BaseScreen;
import androidgames.networkgames.screens.components.MenuItem.IEventListener;

public class NativeMenu extends DrawableComponent implements IEventListener {

	private boolean isShow = true;
	private ArrayList<DrawableComponent> menu;
	private boolean vertical = false;
	private static int menuSize = 55;
	private static int buttonSize = 45;
	private DrawableButton btnShow;
	private int padding = 10;
	private ArrayList<MenuItem> item;
	private int srX = 0;
	private int itemWidth = 0;
	private boolean touch = false;
	private int itemTuoch = -1;
	private Sound sound = null;
	private Paint paint;

	/**
	 * Native menu
	 * 
	 * @screen the parent screen which is call this menu
	 * @X X-Axis position
	 * @Y Y-Axis position
	 * */
	public NativeMenu(BaseScreen screen, int X, int Y) {
		super(screen, Color.GRAY, menuSize, menuSize, X, Y, menuSize, menuSize);
		this.screen = screen;
		paint = new Paint();
		sound = screen.getGame().getAudio().newSound("Sounds/scroll.mp3");
		menu = new ArrayList<DrawableComponent>();
		btnShow = new DrawableButton(screen, Color.TRANSPARENT, buttonSize,
				buttonSize, 0, 0, 0, 0, "  ", 0, 0) {
			@Override
			public void present(float deltatime, Graphics g) {
				// TODO Auto-generated method stub
				if (btnShow.background != null) {
					Bitmap bg = Bitmap.createScaledBitmap(
							((AndroidPixmap) btnShow.background).bitmap,
							buttonSize, buttonSize, true);
					btnShow.background = new AndroidPixmap(bg,
							PixmapFormat.RGB565);
					g.drawPixmap(btnShow.background, iX, iY, iSourceX,
							iSourceY, iWidth, iHeight);
				} else
					g.drawRect(iX, iY, iWidth, iHeight, iBackgroundColor);
				g.drawText(strButton, iTX, iTY, 0xf8f4ebff, 30, Style.FILL);
			}
		};
		this.menu.add(btnShow);
		iX = X;
		iY = Y;
		srX = 0;
	}

	/**
	 * initial the component UI.
	 */
	private void setup() {
		getSize();
		if (!vertical) {
			setupHor();
		} else if (vertical) {
			setupVer();
		}
	}

	/**
	 * Show the menu UI
	 */
	public void show() {
		this.isShow = true;
		if (srX == menuSize + padding * 2)
			srX = menuWidth();
		btnShow.strButton = "  ";
	}

	/** Hide the menu UI */
	public void hide() {
		this.isShow = false;
		if (srX == menuWidth())
			srX = menuSize + padding * 2;
		btnShow.strButton = "  ";
	}

	/** Setup size for menu */
	private void getSize() {
		// iWidth = 0;
		// iHeight = 0;
		if (!vertical) {
			iHeight = menuSize;
			iWidth = menuSize + padding;
		} else if (vertical) {
			iHeight = menuSize;
			iWidth = menuSize + padding;
		}
	}

	/** Not implement yet */
	private void setupVer() {

	}

	/** Not implement yet */
	private void setupHor() {

	}

	/** Set menu show full Screen Width with horizontal menu. Not implement yet. */
	public void Full() {
	}

	/** Not implement yet */
	public void Vertical() {
		// vertical = true;
	}

	@Override
	/**Click to show or hide menu*/
	public boolean update(float deltatime, TouchEvent touchevents) {
		// TODO Auto-generated method stub
		if (touchevents.type == TouchEvent.TOUCH_UP) {
			if (touchevents.x > btnShow.iX && touchevents.y > btnShow.iY
					&& touchevents.x < btnShow.iX + btnShow.iWidth
					&& touchevents.y < btnShow.iY + btnShow.iHeight) {
				CEvent cevent = new CEvent();
				cevent.iEventType = CEvent.TOUCH;
				fireEvent(cevent);
				// TODO Show or hide menu
				if (isShow) {
					hide();
					sound.play(1);
				} else if (!isShow) {
					show();
					sound.play(1);
				}
				return true;
			} else {
				for (int i = 0; i < item.size(); i++) {
					if (touchevents.x > item.get(i).iX + iX + menuSize
							+ padding * 2
							&& touchevents.y > item.get(i).iY + iY
							&& touchevents.x < item.get(i).iX + iX + menuSize
									+ padding * 2 + item.get(i).iWidth
							&& touchevents.y < item.get(i).iY + iY
									+ item.get(i).iHeight) {
						TouchEvent cevent = new TouchEvent();
						cevent.type = TouchEvent.TOUCH_UP;
						item.get(i).addTouchListener(this);
						item.get(i).fireEvent(cevent);
						touch = false;
						itemTuoch = -1;
						return true;
					}
				}
			}
		} else if (touchevents.type == TouchEvent.TOUCH_DOWN) {
			for (int i = 0; i < item.size(); i++) {
				if (touchevents.x > item.get(i).iX + iX + menuSize + padding
						* 2
						&& touchevents.y > item.get(i).iY + iY
						&& touchevents.x < item.get(i).iX + iX + menuSize
								+ padding * 2 + item.get(i).iWidth
						&& touchevents.y < item.get(i).iY + iY
								+ item.get(i).iHeight) {
					TouchEvent cevent = new TouchEvent();
					item.get(i).addTouchListener(this);
					item.get(i).fireEvent(cevent);
					touch = true;
					itemTuoch = i;
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public void present(float deltatime, Graphics g) {
		paint.setColor(iBackgroundColor);
		paint.setAlpha(150);
		if (isShow) {
			btnShow.background = g.newPixmap(UIConfig.BUTTON_SHOW,
					PixmapFormat.RGB565);
		} else if (!isShow) {
			btnShow.background = g.newPixmap(UIConfig.BUTTON_HIDE,
					PixmapFormat.RGB565);
		}
		/** Call function setup menu UI */
		setup();
		/** draw background menu */
		if (background != null) {
			g.drawPixmap(background, iX, iY);
		} else {
			g.drawRect(iX, iY, iWidth, menuSize, paint);
		}
		Pixmap bgPix = g
				.newPixmap(UIConfig.MENU_BUTTON_BG, PixmapFormat.RGB565);
		/**
		 * get main button at the current time only one main button
		 */
		int m_width = iX;
		DrawableComponent component = menu.get(0);
		if (!vertical) {
			component.iX = m_width + padding;
			component.iY = iY + padding / 2;
			// component.background = bgPix;
			if (component instanceof DrawableButton) {
				((DrawableButton) component).iTX = component.iX + padding;
				((DrawableButton) component).iTY = component.iY + buttonSize
						- padding;
			}
			component.iHeight = buttonSize;
			if (component.iWidth < buttonSize)
				component.iWidth = buttonSize;
			m_width = component.iX + component.iWidth;
			this.iWidth = m_width + padding * 2;
		}
		/** Show main button */
		component.present(deltatime, g);

		/** create buffer bitmap */
		Bitmap bitmap = Bitmap.createBitmap(menuWidth(), menuSize,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		paint.setAlpha(200);
		Bitmap bgBit = Bitmap.createScaledBitmap(
				((AndroidPixmap) bgPix).bitmap, menuWidth(), menuSize, true);
		canvas.drawBitmap(bgBit, 0, 0, paint);
		/** draw all item to buffer bitmap */
		menuWidth();
		for (int i = 0; i < item.size(); i++) {
			if (touch && i == itemTuoch) {
				Pixmap pix = g.newPixmap(UIConfig.ITEM_MENU_AL,
						PixmapFormat.RGB565);
				Bitmap bit = Bitmap.createScaledBitmap(
						((AndroidPixmap) pix).bitmap, item.get(i).width,
						buttonSize, true);
				canvas.drawBitmap(bit, item.get(i).iX, padding / 2, paint);
			} else if (item.get(i).background == null) {
				Pixmap pix = g.newPixmap(UIConfig.ITEM_MENU,
						PixmapFormat.RGB565);
				Bitmap bit = Bitmap.createScaledBitmap(
						((AndroidPixmap) pix).bitmap, item.get(i).width,
						buttonSize, true);
				canvas.drawBitmap(bit, item.get(i).iX, padding / 2, paint);
			} else if (item.get(i).background != null) {
				Bitmap bit = Bitmap.createScaledBitmap(
						((AndroidPixmap) item.get(i).background).bitmap,
						item.get(i).width, buttonSize, true);
				canvas.drawBitmap(bit, item.get(i).iX, padding / 2, paint);
			}
			paint.setColor(Color.WHITE);
			paint.setAlpha(255);
			canvas.drawText(item.get(i).text, item.get(i).iTX,
					padding + paint.getTextSize(), paint);
		}
		/** draw bitmap in screen */
		if (isShow) {
			if (srX > 0) {
				srX -= bitmap.getWidth() * deltatime;
				g.drawPixmap(new AndroidPixmap(bitmap, PixmapFormat.RGB565), iX
						+ menuSize + padding * 2, iY, srX, 0,
						bitmap.getWidth(), bitmap.getHeight());
			} else if (srX <= menuSize) {
				srX = 0;
				g.drawPixmap(new AndroidPixmap(bitmap, PixmapFormat.RGB565), iX
						+ menuSize + padding * 2, iY, srX, 0,
						bitmap.getWidth(), bitmap.getHeight());
			}

		} else if (!isShow) {
			if (srX < bitmap.getWidth()) {
				srX += bitmap.getWidth() * deltatime;
				g.drawPixmap(new AndroidPixmap(bitmap, PixmapFormat.RGB565), iX
						+ menuSize + padding * 2, iY, srX, 0,
						bitmap.getWidth(), bitmap.getHeight());
			}
		}
	}

	/**
	 * Add item
	 * 
	 * @menuitem item will be added in type of MenuItem
	 */
	public void addItem(MenuItem menuitem) {
		MenuItem item = new MenuItem("", null);
		item = menuitem;
		if (item.text == null || item.text.length() < 1) {
			item.text = "    ";
		}
		if (this.item == null) {
			this.item = new ArrayList<MenuItem>();
			this.item.add(item);
		} else
			this.item.add(item);
	}

	@Override
	public void onTouch(MenuItem menuItem, TouchEvent event) {

	}

	/** get menu width without main button */
	public int menuWidth() {
		int menuWidth = 0;
		/** get total item width */
		for (int i = 0; i < item.size(); i++) {
			item.get(i).width = (int) paint.measureText(item.get(i).text
					.toString()) + padding * 2;
			if (this.itemWidth > 0)
				item.get(i).width = this.itemWidth;
			item.get(i).height = (int) paint.getTextSize();
			if (i == 0) {
				item.get(i).iX = padding;
				item.get(i).iY = padding / 2;
			}
			if (i > 0) {
				item.get(i).iX = item.get(i - 1).iX + item.get(i - 1).width
						+ padding * 2;
				item.get(i).iY = padding / 2;
			}
			item.get(i).iTX = item.get(i).iX + padding;
			item.get(i).iWidth = item.get(i).width;
			item.get(i).iHeight = item.get(i).height;

			menuWidth += item.get(i).width + padding * 2;
		}
		return menuWidth;
	}

	/**
	 * set the same width for all item. Not implements yet.
	 */
	public void setItemWidth(int itemWidth) {
		this.itemWidth = itemWidth;
	}

}
